/*
 * @Author: ChunLai
 * @Date: 2023-08-09 16:09:12
 * @LastEditTime: 2025-05-22 19:33:43
 * @Description:需求分析问卷(重庆告知)
 * @FilePath: \02.bldinsure\src\translate\healthy\chongqing.js
 */
// 校验规则：需求分析问卷必须填写完整，否则则出校验提示：根据地方保监要求，请您完整填写需求分析问卷xxx条   "根据地方保监要求，请您完整填写需求分析问卷1条"

export const validateChongqing = (datas, showedRisks, totalPrem) => {
  // console.log('需求分析问卷', datas);
  let obj = {
    msg: "",
    status: true,
  };
  /* === 1.填写不完整校验 === */
  const q1 = datas[0];
  // 第1(1)题:勾选完A、B选项后还必须勾选后面的某一条或多条
  if (!q1.optionList[0].checked && !q1.optionList[1].checked) {
    return setError(q1.errMsg);
  } else if (q1.optionList[0].checked && q1.optionA.length == 0) {
    return setError(q1.errMsg);
  }
  if (q1.optionList[1].checked) {
    if (q1.optionB.length == 0) {
      // 第1(1)题 B
      return setError(q1.errMsg);
    } else if (datas[1].ImpartParammodle == "") {
      // 第1(2)题
      return setError(datas[1].errMsg);
    } else if (datas[2].ImpartParammodle == "") {
      // 第1(3)题
      return setError(datas[2].errMsg);
    }
  }
  /* === 2.第2题校验 === */
  const q2 = datas[3];
  let sub2 = false;
  const sub2list = q2.optionList,
    len2 = sub2list.length;
  for (let i = 0; i < len2; i++) {
    const ele = sub2list[i];
    if (ele.checked) {
      sub2 = true;
      break;
    }
  }
  if (!sub2) {
    return setError(q2.errMsg);
  }
  for (let i = 0; i < len2; i++) {
    const ele = sub2list[i];
    if (
      ele.checked &&
      (ele.inputCash === "" || ele.inputTime === "" || ele.inputYear === "")
    ) {
      return setError(`${q2.errMsg}-${ele.label}内容`);
    }
    if (
      !ele.checked &&
      (ele.inputCash !== "" || ele.inputTime !== "" || ele.inputYear !== "")
    ) {
      return setError(`${q2.errMsg}-${ele.label}填写内容时,请勾选该项`);
    }
    // 填写的XX年/周岁，需校验只能是数字且大于0，最大不能超过105
    if (ele.checked && Number(ele.inputYear) > 105) {
      return setError(`${q2.errMsg}-${ele.label}:缴费年期最大不能超过105`);
    }
  }
  /* === 3.第3题校验 === */
  if (datas[4].ImpartParammodle == "") {
    return setError(datas[4].errMsg);
  }
  /* === 4.第4题校验 === */
  let sub4 = false;
  const sub4list = datas[5].optionList,
    len4 = sub4list.length;
  for (let i = 0; i < len4; i++) {
    const ele = sub4list[i];
    if (ele.checked) {
      sub4 = true;
      break;
    }
  }
  if (!sub4) {
    return setError(datas[5].errMsg);
  }
  for (let i = 0; i < len4; i++) {
    const ele = sub4list[i];
    if (ele.checked && (ele.inputCash === "" || ele.inputTime === "")) {
      return setError(datas[5].errMsg);
    }
  }

  const mainPayendyear = showedRisks[0].payendyear;
  /* === 3.核保规则校验--第4题 === */
  // 3-1.交费方式按主险判断，按投保单首期保费（主险+所附加的附加险保费）进行判断
  const findSub4 = sub4list.find((ele) => ele.checked),
    payType = mainPayendyear === "趸交" ? "A" : "B";
  if (payType !== findSub4.name) {
    return setError("投保产品交费方式与需求分析问卷中 第4题 选择的不符");
  }
  // 3-2.投保产品的交费水平必须小于或等于客户保费预算，否则出校验提示：投保产品交费水平大于或等于客户保费预算
  if (Number(findSub4.inputCash) > Number(totalPrem)) {
    return setError("投保产品交费水平大于或等于需求分析问卷中填写的保费预算");
  }
  if (
    findSub4.name == "B" &&
    Number(findSub4.inputTime) > Number(mainPayendyear)
  ) {
    return setError(
      "投保单的交费年期必须小于或等于需求分析问卷中客户能够持续交费的年限"
    );
  }
  return obj;
};

export const pageToApiChongqing = (healthyDatas) => {
  // console.log('pageToApiChongqing', healthyDatas);
  let QueInfo = [];

  for (let i = 0; i < healthyDatas.length; i++) {
    const item = healthyDatas[i];
    let obj = {
      QueVersion: item.QueVersion,
      QueCode: item.QueCode,
      QueRemarkInfo: [],
    };
    if (item.type === "subject1") {
      if (item.optionA.length > 0) {
        let obj1 = {
          QueRemark: "A",
          QueContent: item.optionA.join(","),
        };
        obj.QueRemarkInfo.push(obj1);
      }
      if (item.optionB.length > 0) {
        let obj11 = {
          QueRemark: "B",
          QueContent: item.optionB.join(","),
        };
        obj.QueRemarkInfo.push(obj11);
      }
      QueInfo.push(obj);
    } else if (["subject1_2", "subject1_3", "subject3"].includes(item.type)) {
      if (item.ImpartParammodle !== "") {
        let obj12 = {
          QueRemark: item.ImpartParammodle,
          QueContent: "",
        };
        obj.QueRemarkInfo = [obj12];
      }
      QueInfo.push(obj);
    } else if (item.type === "subject2") {
      const sub2Opt = item.optionList,
        sub2OptLen = sub2Opt.length;
      obj.QueRemarkInfo = [];
      for (let j = 0; j < sub2OptLen; j++) {
        const sub2Item = sub2Opt[j];
        if (sub2Item.checked) {
          let obj2 = {
            QueRemark: sub2Item.name,
            QueContent: `${sub2Item.inputCash},${sub2Item.inputYear},${sub2Item.inputTime}`,
          };
          obj.QueRemarkInfo.push(obj2);
        }
      }
      QueInfo.push(obj);
    } else if (item.type === "subject4") {
      const sub4Opt = item.optionList,
        sub4OptLen = sub4Opt.length;
      obj.QueRemarkInfo = [];
      for (let j = 0; j < sub4OptLen; j++) {
        const sub4Item = sub4Opt[j];
        if (sub4Item.checked) {
          let obj4 = {
            QueRemark: sub4Item.name,
            QueContent: sub4Item.inputTime
              ? `${sub4Item.inputCash},${sub4Item.inputTime}`
              : `${sub4Item.inputCash}`,
          };
          obj.QueRemarkInfo.push(obj4);
        }
      }
      QueInfo.push(obj);
    }
  }
  // console.log('api', QueInfo);
  return QueInfo;
};

export const apiToPageChongqing = (healthyDatas, echoDatas) => {
  for (let k = 0; k < healthyDatas.length; k++) {
    const ele = healthyDatas[k];
    if (ele.type === "subject1") {
      const findItem = echoDatas.find((obj) => obj.QueCode === ele.QueCode);
      for (let i = 0; i < ele.optionList.length; i++) {
        const sub = ele.optionList[i];
        const find2 = findItem.QueRemarkInfo.find(
          (obj) => obj.QueRemark === sub.name
        );
        if (find2 && Object.keys(find2).length > 0) {
          sub.checked = true;
          ele[`option${sub.name}`] = find2.QueContent.split(",");
        }
      }
    } else if (["subject1_2", "subject1_3", "subject3"].includes(ele.type)) {
      const findItem = echoDatas.find((obj) => obj.QueCode === ele.QueCode);
      // console.log(ele.type,findItem)
      if (
        findItem &&
        findItem.QueRemarkInfo &&
        findItem.QueRemarkInfo.length > 0
      ) {
        ele.ImpartParammodle = findItem.QueRemarkInfo[0].QueRemark;
      }
    } else if (ele.type === "subject2") {
      const findItem = echoDatas.find((obj) => obj.QueCode === ele.QueCode);
      for (let i = 0; i < ele.optionList.length; i++) {
        const sub = ele.optionList[i];
        const find2 = findItem.QueRemarkInfo.find(
          (obj) => obj.QueRemark === sub.name
        );
        if (find2 && Object.keys(find2).length > 0) {
          sub.checked = true;
          const arr1 = find2.QueContent.split(",");
          sub.inputCash = arr1[0];
          sub.inputYear = arr1[1];
          sub.inputTime = arr1[2] || "Y";
        }
      }
    } else if (ele.type === "subject4") {
      const findItem = echoDatas.find((obj) => obj.QueCode === ele.QueCode);
      for (let i = 0; i < ele.optionList.length; i++) {
        const sub = ele.optionList[i];
        const find2 = findItem.QueRemarkInfo.find(
          (obj) => obj.QueRemark === sub.name
        );
        if (find2 && Object.keys(find2).length > 0) {
          sub.checked = true;
          const arr1 = find2.QueContent.split(",");
          sub.inputCash = arr1[0];
          if (sub.inputTime !== undefined) {
            sub.inputTime = arr1[1];
          }
        }
      }
    }
  }
  return healthyDatas;
};

export const jsonInitPageDatas = (initDatas, showedRisks, totalPrem) => {
  // 特殊字段初始赋值
  const findEle1 = initDatas.find((ele) => ele.type === "subject1"),
    findEle3 = initDatas.find((ele) => ele.type === "subject3"),
    findEle4 = initDatas.find((ele) => ele.type === "subject4"),
    mainRisk = showedRisks[0],
    mainPayYear = mainRisk.payendyear,
    mainPayYearType = mainRisk.payendyearType,
    totalFee = totalPrem;
  findEle1.optionList[0].checked = true;
  findEle1.optionA = ["b"];
  findEle3.ImpartParammodle = "D";
  // 第4题
  if (mainPayYearType === "Y") {
    findEle4.optionList[1].checked = true;
    findEle4.optionList[1].inputCash = totalFee;
    findEle4.optionList[1].inputTime =
      mainPayYear === "趸交" ? "1" : mainPayYear;
  } else {
    findEle4.optionList[0].checked = true;
    findEle4.optionList[0].inputCash = totalFee;
  }
  return initDatas;
};

// 工具函数：设置错误信息并返回结果对象
const setError = (msg) => {
  result.msg = msg;
  result.status = false;
  return result;
};
